package service

import (
	"log"
	"prometheus_import_consul/global"
	"prometheus_import_consul/tools"
)

// ImportServiceURLs 使用map读取并解析JSON文件，然后添加服务
func ImportServiceURLs() {
	// 读取JSON文件

	// 使用map解析JSON数据
	var serviceURLs []map[string]interface{}
	err := tools.ReadJSONFile(global.Config.JsonFile.ServiceUrljson_Path, &serviceURLs)
	if err != nil {
		log.Fatalf("Error reading adn parsing JSON file: %s,error: %v\n", global.Config.JsonFile.ServiceUrljson_Path, err)
	}
	log.Printf("Success reading and parsing JSON file: %s\n", global.Config.JsonFile.ServiceUrljson_Path)

	// 遍历解析结果并添加服务
	for _, serviceURL := range serviceURLs {
		// 进行类型断言以确保数据类型正确
		nameInterface, _ := serviceURL["nameInterface"].(string)
		urlInterface, _ := serviceURL["urlInterface"].(string)
		requestMethod, _ := serviceURL["requestMethod"].(string)
		serviceType, _ := serviceURL["serviceType"].(string)
		contentType, _ := serviceURL["contentType"].(string)
		requestClientId, _ := serviceURL["requestClientId"].(string)
		requestToken, _ := serviceURL["requestToken"].(string)
		requestBody, _ := serviceURL["requestBody"].(string)
		responseValue, _ := serviceURL["responseValue"].(string)
		env, _ := serviceURL["env"].(string)
		businessCode, _ := serviceURL["businessCode"].(string)

		// 调用Add_interface函数，将解析的数据作为参数传递
		tools.Add_Interface(nameInterface, urlInterface, requestMethod, contentType, requestClientId, requestToken, requestBody, responseValue, env, serviceType, businessCode)
	}
}
